package uk.co.kevinlockwood.kol.pages;

import java.util.List;

import uk.co.kevinlockwood.kol.JavaHelpers;
import uk.co.kevinlockwood.kol.droid.DroidHelpers;

public class LoginPage extends KoLPage {
	public enum LoginResult {
		NOT_LOGGED_IN,
		NOT_ONLINE,
		WRONG_PASSWORD,
		ROLL_OVER,
		ALREADY_LOGGED_IN,
		UNKNOWN,
		SUCCESS;

		public String id = null;
	}

	private String user;
	private String challenge;
	private String response;
	private LoginResult result;
	private String appserver;
	private String phpsessid;

	public LoginPage(String user, String challenge, String response) {
		this.user = user;
		this.challenge = challenge;
		this.response = response;
		this.result = LoginResult.NOT_LOGGED_IN;
		this.appserver = null;
		this.phpsessid = null;
	}

	public String getChallenge() {
		return this.challenge;
	}
	public LoginResult getLoginResult() {
		return result;
	}
	public void setResponse(String response) {
		this.response = response;
		this.appserver = null;
	}
	public LoginResult getResult() {
		return result;
	}

	@Override
	public String pageName() {
		StringBuilder s = new StringBuilder("login.php");
		if (user != null && challenge != null && response != null) {
			s.append("?loggingin=Yup.&loginname=");
			s.append(user);
			s.append("&secure=1&challenge=");
			s.append(challenge);
			s.append("&password=&response=");
			s.append(response);
		}
		return s.toString();
	}

	@Override
	public Boolean readHeader(String name, String value) {
		if ("set-cookie".equals(name)) {
			int i = value.indexOf("appserver=");
			if (i >= 0) {
				int j = value.indexOf(";");
				if (j > 0) {
					appserver = value.substring(i, j);
				} else {
					appserver = value.substring(i);
				}
			}
			i = value.indexOf("PHPSESSID");
			if (i >= 0) {
				int j = value.indexOf(";");
				if (j > 0) {
					phpsessid = value.substring(i, j);
				} else {
					phpsessid = value.substring(i);
				}
			}
			if (appserver != null && phpsessid != null) {
				result = LoginResult.SUCCESS;
				result.id = phpsessid + "; " + appserver;
				DroidHelpers.debug("got session cookie [",result.id,"]");
			}
		}
		return result != LoginResult.SUCCESS;
	}

	@Override
	public void buildFromHtml(List<String> html) {
		if (challenge == null) {
			// get the challenge info
			findChallenge(html);
		} else if (result.id == null) {
			// hmmm we should have one of those, wonder what went wrong?
			findError(html);
		}
	}

	private void findChallenge(List<String> html) {
		for (String s : html) {
			challenge = JavaHelpers.substringToString(s, "challenge value=\"", "\"");
			if (challenge != null) {
				DroidHelpers.debug("found challenge [",challenge,"]");
				break;
			}
		}
	}

	private void findError(List<String> html) {
		result = LoginResult.UNKNOWN;
		for (String s : html) {
			if (s.indexOf("Whoops") > 0) {
				result = LoginResult.ALREADY_LOGGED_IN;
				break;
			} else if (s.indexOf("Bad password.") > 0) {
				result = LoginResult.WRONG_PASSWORD;
				break;
			} else if (s.indexOf("Too many login attempts") > 0) {
				result = LoginResult.ALREADY_LOGGED_IN;
				break;
			}
		}
	}

	/*	-- ALREADY LOGGED IN MESSAGE --
	 * <center><table  width=500  cellspacing=0 cellpadding=0><tr><td style="color: white;" align=center bgcolor=blue><b>
	 * <font face=arial>The Kingdom of Loathing</font></b></td></tr><tr><td style="padding: 5px; border: 1px solid blue;"><center>
	 * <table><tr><td><font face=arial>Whoops -- it looks like you had a recent session open that didn't get logged out of properly.
	 * We apologize for the inconvenience, but you'll need to wait a couple of minutes before you can log in again.<p><center><a
	 * href="login.php"><font color=blue>Back to Login</font></a></center></font></td></tr></table></center></td></tr><tr><td
	 * height=4></td></tr></table></center>
	 */

	@Override
	public Boolean followRedirects() {
		// if we're getting the challenge, we should follow redirects
		return challenge == null;
	}
}
